//
//  HTSearchResultCell.swift
//  MagaCore
//
//  Created by MrMessy on 2023/5/7.
//

import SwiftUI

let var_gridItemWidth: CGFloat = (UIDevice.ht_screenWidth() - 60 - 40) / 6
let var_gridItemHeight: CGFloat = var_gridItemWidth * 0.64

struct HTSearchResultCell: View {

  @Binding var var_type: String
  @Binding var var_searchText: String
  @State var var_searchAnime: HTAnime = HTAnime()
  @State private var var_columns: [GridItem] = [GridItem(.adaptive(minimum: var_gridItemWidth, maximum: var_gridItemWidth))]
  @State private var var_selEp: HTEp = HTEp()
  @State private var var_showPushToVideoDetail: Bool = false
  @State private var var_showPushToEpisodes: Bool = false

  var body: some View {
    ZStack{
      NavigationLink(destination: HTVideoDetailScreen(var_id: var_searchAnime.var_id, var_epId: var_selEp.var_id ?? "", var_source: "4"), isActive: $var_showPushToVideoDetail) {
        EmptyView()
      }
      NavigationLink(destination: HTEpisodesScreen(var_id: var_searchAnime.var_id), isActive: $var_showPushToEpisodes) {
        EmptyView()
      }

      VStack(alignment: .leading, spacing: 0) {
        HStack(spacing: 10){
          HTCoverView(var_anime: var_searchAnime, var_width: UIDevice.ht_fixToWidth(width: 76), var_height: UIDevice.ht_fixToWidth(width: 96))
          VStack(alignment: .leading, spacing: 5){
            Text(var_searchAnime.var_title ?? "")
              .foregroundColor(Color(hex: "#393939"))
              .font(.system(size: UIDevice.ht_fixToWidth(width: 14)))
              .fontWeight(.bold)
              .lineLimit(1)
              .padding(.trailing, 50)
              .padding(.top, 5)
              .multilineTextAlignment(.leading)
              .frame(maxWidth: .infinity, alignment: .topLeading)
            if (var_searchAnime.var_title_2 != nil && var_searchAnime.var_title_2 != "") {
              Text(var_searchAnime.var_title_2 ?? "")
                .font(.system(size: UIDevice.ht_fixToWidth(width: 10)))
                .lineLimit(1)
                .foregroundColor(Color(hex: "#999999"))
            }
            Text(var_searchAnime.var_pub_date ?? "")
              .font(.system(size: UIDevice.ht_fixToWidth(width: 10)))
              .lineLimit(1)
              .foregroundColor(Color(hex: "#999999"))
            Spacer()
            Text(var_searchAnime.ht_searchTag())
              .font(.system(size: UIDevice.ht_fixToWidth(width: 10)))
              .foregroundColor(Color(hex: "#FF6C3C"))
              .padding(.bottom, 5)
          }
        }
        .padding(.trailing, 15)
        .padding(.vertical, 15)
          /**
        if (var_searchAnime.var_ep != "Ep Full") {
          LazyVGrid(columns: var_columns, spacing: 5) {
            ForEach((0..<var_searchAnime.ht_searchEps().count), id: \.self) {index in
              let var_ep = var_searchAnime.ht_searchEps()[index]
              Button {
                if (var_ep != nil) {
                  var_selEp = var_ep!
                  var_showPushToVideoDetail = true
                } else {
                  var_showPushToEpisodes = true
                }
                HTNetworkManager().ht_requestForBuried(.htEnumSearchResultClNew(var_kid: "\(13 + index)", var_item_id: var_searchAnime.var_id, var_type: var_type, var_word: var_searchText))
              } label: {
                ZStack(alignment: .topTrailing){
                  HStack{
                    Text("\(var_ep != nil ? var_ep!.var_eps_num ?? "" : "...")")
                      .font(.system(size: UIDevice.ht_fixToWidth(width: 14)))
                      .ht_normal()
                  }
                  .frame(maxWidth: .infinity, maxHeight: .infinity)

                  if (var_ep?.var_new_flag == "1") {
                    HStack{
                      Text("New")
                        .font(.system(size: UIDevice.ht_fixToWidth(width: 7)))
                        .foregroundColor(Color.white)
                    }
                    .frame(width: UIDevice.ht_fixToWidth(width: 20), height: UIDevice.ht_fixToWidth(width: 10))
                    .background(Color(hex: "#FC0B52"))
                    .clipShape(HTRroundedCorners(var_bl: UIDevice.ht_fixToWidth(width: 4)))
                  }
                }
                .frame(minWidth: var_gridItemWidth, maxWidth: .infinity, minHeight: var_gridItemHeight, alignment: .topTrailing)
                .background(Color.black.opacity(0.04))
                .cornerRadius(UIDevice.ht_fixToWidth(width: 4))
              }
            }
          }
          .padding(.trailing, 15)
          .padding(.bottom, 15)
        }
          */
      }
      .padding(.leading, 15)
      .background(Color.white)
    }
  }
}
